﻿using System;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Controls;

namespace YArchitech.Revit
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public abstract class HYExternalApplication : IExternalApplication
	{
		public UIControlledApplication App
		{
			get
			{
				return this.app;
			}
		}

		public abstract Result HYOnStartup();

		public abstract Result HYOnShutdown();

		public Result OnStartup(UIControlledApplication application)
		{
			this.app = application;
			Result result = -1;
			try
			{
				result = this.HYOnStartup();
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				YJKMessageBox.Information("操作已取消");
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			catch (Exception ex)
			{
				YJKMessageBox.Information(ex.Message);
				return Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				this.TailProcess();
			}
			return result;
		}

		public Result OnShutdown(UIControlledApplication application)
		{
			return this.HYOnShutdown();
		}

		public void TailProcess()
		{
		}

		private UIControlledApplication app;
	}
}
